﻿using Showme.Data;
using Showme.Data.Objects;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Showme.Controllers
{
    public class ShowmeController
    {
        private DataAccessLayer data;
        private static ShowmeController _instance; // singleton instance
        public User currentUser { get; private set; }

        // forms
        public LoginForm loginForm;
        public ManagerMain mainForm;

        private ShowmeController()
        {
            data = new DataAccessLayer();
        }

        /* get singleton instance */
        public static ShowmeController Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ShowmeController();

                return _instance;
            }
        }

        public DataTable getUsers(UserType userType = UserType.All)
        {
            return data.getAllUsers(userType);
        }

        public UserTypeBoxItem[] getUserTypes()
        {
            var userTypes = data.getUserTypes();
            return data.getUserTypes().AsEnumerable().Select(s => new UserTypeBoxItem((int)s[0], s[1].ToString())).ToArray();
        }

        public DataRow getUser(string userid)
        {
            return data.getUser(userid);
        }

        /*public DataRow saveUser(string userid, string firstName, string lastName, string degree, string username, string password, int userType, string gender)
        {
            data.saveUser(userid, firstName, lastName, degree, username, password, userType, gender);
            return getUser(userid);
        }*/

        public bool login(string username, string password)
        {
            var datarow = data.getUser(username, password);
            if (datarow != null)
            {
                currentUser = new User(identity: datarow["fld_userID"].ToString(),
                    username: datarow["fld_username"].ToString(),
                    userType: (UserType)Int32.Parse(datarow["fld_userType"].ToString()));
                return true;
            }
            else
            {
                return false;
            }
        }

        /*public void saveMedia(string name, string genre, string link)
        {
            data.saveMedia(name, genre, link);
        }*/


        internal DataTable GetMedia()
        {
            return data.GetAllMedia();
        }
    }
}
